package com.dangdang.myspring.testProxy;

import com.dangdang.myspring.testProxy.service.service1;
import com.dangdang.myspring.testProxy.service.service2;
import com.dangdang.myspring.testProxy.service.service4;
import org.apache.commons.lang3.builder.ToStringExclude;
import org.junit.Test;
import org.springframework.cglib.proxy.*;

import java.lang.reflect.Method;

public class CglibTest1 {
    @Test
    public void test1(){

        Enhancer  enhancer = new Enhancer();

        enhancer.setSuperclass(service1.class);

        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("调用方法："+method);
                Object result = methodProxy.invokeSuper(o,objects);
                return result;
            }
        });

        service1 proxy = (service1) enhancer.create();
        proxy.m1();
        proxy.m2();

    }


    @Test
    public void test2() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(service2.class);
        enhancer.setCallback((MethodInterceptor) (o, method, objects, methodProxy) -> {
            System.out.println("调用方法"+method);
            Object result = methodProxy.invokeSuper(o,objects);
            return result;
        });


        service2 proxy = (service2) enhancer.create();
        proxy.m1();
    }

    @Test
    public void test4(){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(service4.class);
        Callback[] callbacks = {
               new MethodInterceptor() {
                   @Override
                   public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                       long starTime = System.nanoTime();
                       Object result = methodProxy.invokeSuper(o,objects);
                       long endTime = System.nanoTime();
                       System.out.println(method + "，耗时(纳秒):" + (endTime - starTime));
                       return null;
                   }
               },
               new FixedValue() {
                   @Override
                   public Object loadObject() throws Exception {
                        return "返回固定值啦";
                   }
               }
        };


        enhancer.setCallbacks(callbacks);

        enhancer.setCallbackFilter(new CallbackFilter() {
            @Override
            public int accept(Method method) {
                String methodName = method.getName();
                return methodName.startsWith("insert") ? 0:1;
            }
        });

        service4 proxy = (service4) enhancer.create();
        System.out.println("---------------");
        proxy.insert1();
        System.out.println("---------------");
        proxy.insert2();
        System.out.println("---------------");
        System.out.println(proxy.get1());
        System.out.println("---------------");
        System.out.println(proxy.get2());
    }
}
